package graphic.dialogs;

import graphic.FilesTabbedPanel;
import graphic.mycomponents.TextChecked;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import core.Client;

public class NewFileDialog extends AbstractDialog
{
	TextChecked fileName;
	TextChecked programName;
	
	public NewFileDialog()
	{	
		super("Create New File", "Create", 450, 200);
	}
	
	public void createPanel()
	{
		super.setPanel(new JPanel(new GridBagLayout()));
		
		JPanel panel = super.getPanel(); 
		
		this.programName = new TextChecked(FilesTabbedPanel.getIstance().getSelectedProgramName(), TextChecked.TEXT, false, false, null);
		this.fileName = new TextChecked("", TextChecked.TEXT, true, false, null);
			
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(5, 10, 5, 10);
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		panel.add(new JLabel("New Files Creation"), c);
		
		c.gridx = 0;
		c.gridy = 1;
		panel.add(this.programName, c);		

		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		panel.add(this.fileName, c);
		
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 1;
		panel.add(super.getOkButton(), c);

		c.gridx = 1;
		c.gridy = 3;
		panel.add(super.getExitButton(), c);		
	}
	
	@Override
	public boolean checkOkFunction() {
		if(this.fileName.isChecked()) return true;

		JOptionPane.showMessageDialog(this, "Please type a correct file name!", "Error creating a file!", JOptionPane.ERROR_MESSAGE);
		return false;
	}
	
	
	@Override
	public void okFunction()
	{
		Client.getInstance().newFileCreationRequest(this.programName.getText(), this.fileName.getText());	
		super.dispose();
		}
	
}
	